package com.googlecode.imanager.client.value.el;

import com.googlecode.imanager.client.value.ValueType;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class Round extends SingleOperation {

    private static final long serialVersionUID = 1L;
    
    protected double decimals;

    public Round(Expression expression, int decimals) {
        super(expression);
        setDecimals(decimals);
    }

    public int getDecimals() {
        return (int) (Math.log(decimals) / Math.log(10));
    }

    public void setDecimals(int decimals) {
        this.decimals = Math.pow(10, decimals);
    }

    @Override
    public ValueType getValueType() {
        return ValueType.DECIMAL;
    }

    @Override
    public Double getValue() {
        return Math.round(convertToDouble(expression) * decimals) / decimals ;
    }

}
